package com.yootoo.extpmml;

import ml.dmlc.xgboost4j.java.Booster;
import ml.dmlc.xgboost4j.java.DMatrix;
import ml.dmlc.xgboost4j.java.XGBoost;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

public class FinRiskModelV4C {
    public static void main(String[] args) throws Exception {
        // header
        String s1 = "f;main_c0;main_c1;main_c2;main_c3;main_c4;main_c5;main_c6;yqda_c0;yqda_c1;yqda_c2;yqda_c3;yqda_c4;yqda_c5;yqda_c6;yqda_c7;yqda_c8;yqda_c9;yqda_c10;yqda_c11;yqda_c12;yqda_c13;yqda_c14;yqda_c15;yqda_c16;yqda_c17;yqda_c18;yqda_c19;yqda_c20;yqda_c21;yqda_c22;yqda_c23;yqda_c24;yqda_c25;yqda_c26;yqda_c27;yqda_c28;yqda_c29;yqda_c30;yqda_c31;yqda_c32;yqda_c33;yqda_c34;yqda_c35;yqda_c36;yqda_c37;yqda_c38;yqda_c39;yqda_c40;yqda_c41;yqda_c42;yqda_c43;yqda_c44;yqda_c45;yqda_c46;xefq_c0;xefq_c1;xefq_c2;xefq_c3;xefq_c4;xefq_c5;xefq_c6;xefq_c7;xefq_c8;xefq_c9;xefq_c10;xefq_c11;xefq_c12;xefq_c13;xefq_c14;xefq_c15;xefq_c16;xefq_c17;xefq_c18;xefq_c19;xefq_c20;xefq_c21;xefq_c22;xefq_c23;xefq_c24;xefq_c25;xefq_c26;xefq_c27;xefq_c28;xefq_c29;xefq_c30;xefq_c31;xefq_c32;xefq_c33;xefq_c34;xewd_c0;xewd_c1;xewd_c2;xewd_c3;xewd_c4;xewd_c5;xewd_c6;xewd_c7;xewd_c8;xewd_c9;xewd_c10;xewd_c11;xewd_c12;xewd_c13;xewd_c14;xewd_c15;xewd_c16;xewd_c17;xewd_c18;xewd_c19;xewd_c20;xewd_c21;xewd_c22;xewd_c23;xewd_c24;xewd_c25;xewd_c26;xewd_c27;xewd_c28;xewd_c29;xewd_c30;xewd_c31;xewd_c32;xewd_c33;xewd_c34;gzda_c0;gzda_c1;gzda_c2;gzda_c3;gzda_c4;gzda_c5;gzda_c6;gzda_c7;gzda_c8;gzda_c9;gzda_c10;gzda_c11;gzda_c12;gzda_c13;gzda_c14;gzda_c15;gzda_c16;gzda_c17;gzda_c18;gzda_c19;gzda_c20;gzda_c21;gzda_c22;gzda_c23;gzda_c24;gzda_c25;gzda_c26;gzda_c27;gzda_c28;gzda_c29;gzda_c30;gzda_c31;gzda_c32;gzda_c33;gzda_c34;gzda_c35;gzda_c36;gzda_c37;gzda_c38;gzda_c39;gzda_c40;gzda_c41;gzda_c42;gzda_c43;gzda_c44;xwld_c0;xwld_c1;xwld_c2;xwld_c3;xwld_c4;xwld_c5;xwld_c6;xwld_c7;xwld_c8;xwld_c9;xwld_c10;xwld_c11;xwld_c12;xwld_c13;xwld_c14;xwld_c15;xwld_c16;xwld_c17;xwld_c18;xwld_c19;xwld_c20;xyxz_c0;xyxz_c1;xyxz_c2;xyxz_c3;xyxz_c4;xyxz_c5;xyxz_c6;xyxz_c7;xyxz_c8;xyxz_c9;xyxz_c10;xyxz_c11;xyxz_c12;xyxz_c13;xyxz_c14;xyxz_c15;sqld_c0;sqld_c1;sqld_c2;sqld_c3;sqld_c4;sqld_c5;sqld_c6;sqld_c7;sqld_c8;sqld_c9;sqld_c10;sqld_c11;sqld_c12;sqld_c13;end_column";
        // values
         String s2 = "Y;3974ee2afcac63b22a4c23e04d8c65a35fe5edafbf18ea468f7301d5a8abb6fd2018-12-10;3974ee2afcac63b22a4c23e04d8c65a3;5fe5edafbf18ea468f7301d5a8abb6fd;2018-12-10;2018/12/10 17:26;yes;no;3974ee2afcac63b22a4c23e04d8c65a35fe5edafbf18ea468f7301d5a8abb6fd2018-12-10;3974ee2afcac63b22a4c23e04d8c65a3;5fe5edafbf18ea468f7301d5a8abb6fd;2018-12-10;1.0;1.0;800.0;2018-08;1-1000;M0;Y;/;/;/;/;/;/;/;/;/;/;/;/;/;/;/;/;/;/;/;/;/;/;/;/;/;/;/;/;/;/;/;/;/;/;/;/;3974ee2afcac63b22a4c23e04d8c65a35fe5edafbf18ea468f7301d5a8abb6fd2018-12-10;3974ee2afcac63b22a4c23e04d8c65a3;5fe5edafbf18ea468f7301d5a8abb6fd;2018-12-10;536.0;/;/;/;/;/;/;/;/;/;/;/;13;18;0;3;4;17;4.0;1.0;0.0;/;53.85;/;\"\"\"[500\";\"1000)\"\"\";1;5;\"\"\"[0\";\"500)\"\"\";/;3974ee2afcac63b22a4c23e04d8c65a35fe5edafbf18ea468f7301d5a8abb6fd2018-12-10;3974ee2afcac63b22a4c23e04d8c65a3;5fe5edafbf18ea468f7301d5a8abb6fd;2018-12-10;529.0;17400;13200;0;1;2;2;1;57;0;0;0;13;18;0;3;4;17;4.0;1.0;0.0;100.00;85.71;/;0.02;0.00;1;/;/;1;1;3974ee2afcac63b22a4c23e04d8c65a35fe5edafbf18ea468f7301d5a8abb6fd2018-12-10;3974ee2afcac63b22a4c23e04d8c65a3;5fe5edafbf18ea468f7301d5a8abb6fd;2018/12/10;2;2;10000-20000;3000-4000;18-Nov;2.0;2.0;10000-20000;3000-4000;N;18-Oct;4;4;20000-40000;6000-8000;Y;18-Sep;4;4;20000-40000;4000-6000;N;18-Aug;5;5;20000-40000;6000-8000;N;18-Jul;3;3;10000-20000;4000-6000;N;/;/;/;/;/;/;;3974ee2afcac63b22a4c23e04d8c65a32018-12-102018-12-10;3974ee2afcac63b22a4c23e04d8c65a3;5fe5edafbf18ea468f7301d5a8abb6fd;2018-12-10;492.0;76.0;7.0;4.0;1.0;6.0;6.0;0.0;1.0;4.0;6.0;8.0;14.0;1.0;3.0;238.0;2018-11-13;3974ee2afcac63b22a4c23e04d8c65a35fe5edafbf18ea468f7301d5a8abb6fd2018-12-10;3974ee2afcac63b22a4c23e04d8c65a3;5fe5edafbf18ea468f7301d5a8abb6fd;2018-12-10;0.0;0.0;0.0;0.0;0.0;0.0;5300.0;80.0;6.0;6.0;18000.0;7366.0;3974ee2afcac63b22a4c23e04d8c65a35fe5edafbf18ea468f7301d5a8abb6fd2018-12-10;3974ee2afcac63b22a4c23e04d8c65a3;5fe5edafbf18ea468f7301d5a8abb6fd;2018-12-10;530.0;71.0;13.0;1.0;5.0;17.0;2018-12-01;2.0;3.0;16.0" + ";end_column";
        // predict value 0.2126251

//        Scanner scanner = new Scanner(System.in);
//        String s2 = scanner.nextLine() + ";end_column";

        String[] arr1 = s1.split(";");
        String[] arr2 = s2.split(";");
        System.out.println(arr1.length);
        System.out.println(arr2.length);
        StringBuffer stringBuffer = new StringBuffer();
        for (int i = 0; i < arr1.length; ++i) {
            stringBuffer.append("\"" + arr1[i] + "\":\"" + arr2[i] + "\"").append(",");
        }
        stringBuffer.append("endendedn");
        System.out.println("{" + stringBuffer.toString().replace(",endendedn", "") + "}");
        Map<String, Object> naiveFeatures = new HashMap<>();
        for (int i = 0; i < arr1.length; ++i) {
            naiveFeatures.put(arr1[i], arr2[i]);
        }

        // start
        String[] featureNameArr = getFeatureNames();

        HashMap<String, Float> hashMap = getCleanedFeatures(naiveFeatures);
        System.out.println(hashMap.toString());

        System.out.println(Arrays.toString(getFeaturesFromMap(hashMap, featureNameArr)));

        float[] cleanedFeatures = getFeaturesFromMap(hashMap, featureNameArr);
        int colSize = cleanedFeatures.length;
        DMatrix matrix = new DMatrix(cleanedFeatures, 1, colSize, -9999999f);
        Booster boosterModel = XGBoost.loadModel("/Users/admin/Downloads/Work/181214_FinRiskModel/model_fin_v4c.model");
        float[] scoreArray = boosterModel.predict(matrix)[0];
        float score = scoreArray[0];
        System.out.println(score);
    }

    public static float[] getFeaturesFromMap(HashMap<String, Float> hashMap, String[] featureNames) {
        float[] bars = new float[featureNames.length];
        for (int i = 0; i < featureNames.length; ++i) {
            bars[i] = hashMap.get(featureNames[i]);
        }
        return bars;
    }

    public static HashMap<String, Float> getCleanedFeatures(Map<String, Object> naiveFeatures) {
        HashMap<String, Float> hashMap = new HashMap<>();

        String[] feature1 = new String[] {
                "xefq_c4","xefq_c16",
                "xefq_c17","xefq_c18","xefq_c19","xefq_c20","xefq_c21","xefq_c22","xefq_c23",
                "xefq_c24","xefq_c26","xefq_c30","xefq_c31","xewd_c4","xewd_c5",
                "xewd_c6","xewd_c7","xewd_c8","xewd_c9","xewd_c10","xewd_c11","xewd_c12",
                "xewd_c13","xewd_c14","xewd_c15","xewd_c16","xewd_c17","xewd_c18","xewd_c19",
                "xewd_c20","xewd_c21","xewd_c22","xewd_c23","xewd_c24","xewd_c25","xewd_c26",
                "xewd_c28","xewd_c29","xewd_c33","xwld_c4",
                "xwld_c5","xwld_c6","xwld_c7","xwld_c8","xwld_c9","xwld_c10","xwld_c11",
                "xwld_c12","xwld_c13","xwld_c14","xwld_c15","xwld_c16","xwld_c17","xwld_c18",
                "xwld_c19","xyxz_c4","xyxz_c5","xyxz_c6","xyxz_c7","xyxz_c8","xyxz_c9",
                "xyxz_c10","xyxz_c11","xyxz_c12","xyxz_c13","xyxz_c14","xyxz_c15","sqld_c4",
                "sqld_c5","sqld_c6","sqld_c7","sqld_c8","sqld_c9","sqld_c11","sqld_c12",
                "sqld_c13"
        };
        for (String feature : feature1) {
            try {
                Float f = Float.parseFloat(naiveFeatures.get(feature).toString());
                hashMap.put(feature, (float) round(f, 2));
            } catch (Exception e) {
                hashMap.put(feature, -1f);
            }
        }

        String[] feature4 = new String[] {
                "xefq_c28","xefq_c32"
        };
        for (String feature : feature4) {
            String value = naiveFeatures.get(feature).toString();
            value = value.replace("[","");
            value = value.replace("(","");
            value = value.replace("\"","");
            value = value.replace("'","");
            value = value.replace(">","");
            try {
                Float f = Float.parseFloat(value);
                hashMap.put(feature, (float) round(f, 2));
            } catch (Exception e) {
                hashMap.put(feature, -1f);
            }
        }

        return hashMap;
    }

    public static double round(double value, int places) {
        if (places < 0) {
            throw new IllegalArgumentException();
        }

        long factor = (long) Math.pow(10, places);
        value = value * factor;
        long tmp = Math.round(value);
        return (double) tmp / factor;
    }

    public static String arr2String(Object[] arr) {
        StringBuffer stringBuffer = new StringBuffer();
        for (int i = 0; i < arr.length; ++i) {
            stringBuffer.append(arr[i]).append(",");
        }
        stringBuffer.append("endendend");
        return stringBuffer.toString().replace(",endendend", "");
    }

    public static String[] getFeatureNames() {
        String[] arr = new String[]{
                "xefq_c4",
                "xefq_c16",
                "xefq_c17",
                "xefq_c18",
                "xefq_c19",
                "xefq_c20",
                "xefq_c21",
                "xefq_c22",
                "xefq_c23",
                "xefq_c24",
                "xefq_c26",
                "xefq_c28",
                "xefq_c30",
                "xefq_c31",
                "xefq_c32",
                "xewd_c4",
                "xewd_c5",
                "xewd_c6",
                "xewd_c7",
                "xewd_c8",
                "xewd_c9",
                "xewd_c10",
                "xewd_c11",
                "xewd_c12",
                "xewd_c13",
                "xewd_c14",
                "xewd_c15",
                "xewd_c16",
                "xewd_c17",
                "xewd_c18",
                "xewd_c19",
                "xewd_c20",
                "xewd_c21",
                "xewd_c22",
                "xewd_c23",
                "xewd_c24",
                "xewd_c25",
                "xewd_c26",
                "xewd_c28",
                "xewd_c29",
                "xewd_c33",
                "xwld_c4",
                "xwld_c5",
                "xwld_c6",
                "xwld_c7",
                "xwld_c8",
                "xwld_c9",
                "xwld_c10",
                "xwld_c11",
                "xwld_c12",
                "xwld_c13",
                "xwld_c14",
                "xwld_c15",
                "xwld_c16",
                "xwld_c17",
                "xwld_c18",
                "xwld_c19",
                "xyxz_c4",
                "xyxz_c5",
                "xyxz_c6",
                "xyxz_c7",
                "xyxz_c8",
                "xyxz_c9",
                "xyxz_c10",
                "xyxz_c11",
                "xyxz_c12",
                "xyxz_c13",
                "xyxz_c14",
                "xyxz_c15",
                "sqld_c4",
                "sqld_c5",
                "sqld_c6",
                "sqld_c7",
                "sqld_c8",
                "sqld_c9",
                "sqld_c11",
                "sqld_c12",
                "sqld_c13"
        };
        return arr;
    }
}
